import NextName from '@/utils/nextName';

const formatCompress = function formatCompress(str, label, nextName) {
  switch(label) {
    case 'var':
      return str + ' ';
    case 'name':
      return nextName.getName(str);
    default:
      return str;
  }
};

const compressCode = function compressCode(list) {
  let token;
  const arr = [];
  const nextName = new NextName();
  for (token of list) {
    const originStr = token.value != null ? token.value : token.type.label;
    const str = `${formatCompress(originStr, token.type.label, nextName)}`;
    arr.push(str);
  }
  return arr.join('');
};

export default compressCode;
